<template>
  <a-modal
    :title="`${ okHidden ? '' : model && model.id ? '编辑' : '新建'}问卷`"
    :ok-button-props="{ style: okHidden && { display: 'none' } }"
    :width="680"
    :visible="visible"
    :confirmLoading="loading"
    :maskClosable="false"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-if="model && model.id" label="主键ID" hidden>
          <a-input v-decorator="['id', { initialValue: model.id }]" disabled />
        </a-form-item>
        <a-form-item label="主题">
          <a-input :disabled="okHidden" placeholder="请输入主题" v-decorator="['title', { initialValue: model.title, rules: [{required: true, message: '试卷主题不能为空！' }, {max: 28, message: '最大可输入28个字符！' }]}]" />
        </a-form-item>
        <a-form-item label="封面" >
          <Upload :showUploadList="{ showRemoveIcon: !okHidden }" :style="{ marginLeft: !okHidden ? '0px' : '10px' }" :maxLength="1" v-decorator="['img', {rules: [{required: true, message: '请上传封面！' }]}]"></Upload>
          <div :style="{ marginLeft: !okHidden ? '0px' : '10px' }">支持jpg、jpeg、png格式，只能上传一张图片</div>
        </a-form-item>
        <a-form-item label="问卷分类">
          <!-- 别问我为什么不做循环，1是懒 2是这antd-vue 1.x设计的是真xx -->
          <a-select :disabled="okHidden" placeholder="请选择问卷分类" v-decorator="['class_id', {rules: [{required: true, message: '请选择问卷分类！'}]}]" >
            <a-select-option :value="item.id" v-for="item in classList" :key="item.id">
              {{ item.class_name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="问卷状态">
          <a-radio-group :disabled="okHidden" name="exam" v-decorator="['status', { initialValue: model.status, rules: [{required: true, message: '请选择状态！'}]}]">
            <a-radio :value="1">正常</a-radio>
            <a-radio :value="2">停用</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="问卷方式">
          <a-radio-group :disabled="okHidden" v-model="examWay" @change="onChange" v-decorator="['type', { initialValue: model.type, rules: [{required: true, message: '请选择问卷方式！'}]}]">
            <a-radio :style="radioStyle" :value="1">
              随机出题(从题库中拉取指定数量的题目)
              <!-- <a-input v-if="examWay === 1" :size="small" allow-clear /> -->
              <!-- <input type="text" size="s" style="border: 1px solid #ccc;"> -->
            </a-radio>
            <a-radio :style="radioStyle" :value="2">
              固定题目
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="题目数量" v-if="examWay == 1">
          <a-input
            :disabled="okHidden"
            size="small"
            style="width: 120px;"
            placeholder="题目数量"
            v-decorator="['num', { initialValue: model.num, rules: [{required: true, message: '请输入题目数量！'}]}]"/>
        </a-form-item>
        <a-form-item label="问卷时间">
          <a-input size="small" style="width: 120px;" v-show="false" v-decorator="['start_time']"/>
          <a-input size="small" style="width: 120px;" v-show="false" v-decorator="['end_time']"/>
          <a-range-picker :disabled="okHidden" @change="changeDate" v-decorator="['times', {initialValue: model.times}]"/>
        </a-form-item>
        <a-form-item label="问卷细则">
          <a-input :readOnly="okHidden" placeholder="问卷细则" v-show="false" v-decorator="['detailed_rules', { initialValue: model ? model['detailed_rules'] : '' }, {rules: [{required: true, message: '请输入内容！'}]}]" />
          <RichText v-show="!okHidden" ref="richText" :editContent="ruleContent" @btnGetTxt="btnSetTxt" :isLimitNumber="5000"></RichText>
          <div :style="{ marginLeft: okHidden ? '10px' : '0px' }" v-show="okHidden" v-html="ruleContent" class="htm"></div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import Upload from '@/components/Upload/Upload'
import RichText from '@/components/RichText/RichText'
// 表单字段
const fields = ['img', 'class_id', 'title', 'status', 'type', 'num', 'start_time', 'end_time', 'detailed_rules', 'times']
let self
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    okHidden: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    classList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      form: this.$form.createForm(this),
      currentLength: 0,
      examStatus: '1',
      examWay: this.model.type,
      radioStyle: {
        display: 'block',
        height: '30px',
        lineHeight: '30px'
      },
      ruleContent: ''
    }
  },
  watch: {
    'model.detailed_rules': {
      handler (newName, oldName) {
        console.log('数据', newName)
        this.$nextTick(() => {
          self.$refs.richText.init(newName)
        })
      },
      immediate: true
    }
  },
  created () {
    self = this
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    console.log('数据', this.model.detailed_rules)
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
        this.form.setFieldsValue({ type: this.examWay })
      })
    })
  },
  methods: {
    textareaChange () {
      this.form.validateFields((errors, values) => {
        this.currentLength = values.explain.length
      })
    },
    onChange (e) {
      console.log('radio checked', e.target.value)
    },
    changeDate (date, dateString) {
      console.log(date, dateString)
      // this.form.setFieldsValue({ start_time: dateString[0] })
      this.form.setFieldsValue({ start_time: dateString[0] })
      this.form.setFieldsValue({ end_time: dateString[1] })
      // this.form.setFieldsValue({ end_time: dateString[1] })
    },
    btnSetTxt (content) {
      this.ruleContent = content
      this.form.setFieldsValue({ detailed_rules: content })
    }
  },
  components: {
    Upload,
    RichText
  }
}
</script>

<style scoped>
  >>> .ant-upload.ant-upload-select-picture-card {
    margin-bottom: 0
  }
  >>> .clearfix {
    height: 110px
  }
  >>> .remove70 {
    margin-bottom: 110px !important;
    height: 300px !important;
  }
</style>
